package zz.demo.springCloud.config;

import org.springframework.security.web.savedrequest.DefaultSavedRequest;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by zhangzheng on 2017-7-5.
 */
public class CustomInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        HttpSession session=httpServletRequest.getSession();
        session.setAttribute("hello","world");
        String URL=httpServletRequest.getRequestURI();
        String ADDR=httpServletRequest.getRemoteAddr();
        Object requestURL =  session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        if(requestURL!=null){
            DefaultSavedRequest savedRequest = (DefaultSavedRequest) requestURL ;
            System.out.println(savedRequest.getRedirectUrl());
        }else {
            System.out.println("SPRING_SECURITY_SAVED_REQUEST NULL");
        }
//        session.removeAttribute("SPRING_SECURITY_SAVED_REQUEST");
//        session.setAttribute("SPRING_SECURITY_SAVED_REQUEST","http://localhost:9910/");
        System.out.println("--------------preHandle start-------------");
        System.out.println(URL);
        System.out.println("---------------preHandle end--------------");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("--------------postHandle start-------------");
        HttpSession session=httpServletRequest.getSession();
        String URL =httpServletRequest.getRequestURI() ;
        Object requestURL =  session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        if(requestURL!=null){
            DefaultSavedRequest savedRequest = (DefaultSavedRequest) requestURL ;
            System.out.println(savedRequest.getRedirectUrl());
        }else {
            System.out.println("SPRING_SECURITY_SAVED_REQUEST NULL");
        }
        System.out.println(URL);
        System.out.println("---------------postHandle end--------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        HttpSession session=httpServletRequest.getSession();
        String URL = httpServletRequest.getRequestURI();
        System.out.println("--------------afterCompletion start-------------");
        Object requestURL =  session.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
        if(requestURL!=null){
            DefaultSavedRequest savedRequest = (DefaultSavedRequest) requestURL ;
            System.out.println(savedRequest.getRedirectUrl());
        }else {
            System.out.println("SPRING_SECURITY_SAVED_REQUEST NULL");
        }
        System.out.println(URL);
        System.out.println(httpServletResponse.getStatus());
        System.out.println("---------------afterCompletion end--------------");
    }
}
